{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将笔记转化为不同的文件格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import os.path\n",
    "import nbconvert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查路径是否存在："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if not os.path.exists('static files'):\n",
    "    os.mkdir('static files')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "folders = ['01. python tools', \n",
    "           '02. python essentials',\n",
    "           '03. numpy',\n",
    "           '04. scipy',\n",
    "           '05. advanced python',\n",
    "           '06. matplotlib',\n",
    "           '07. interfacing with other languages',\n",
    "           '08. object-oriented programming',\n",
    "           '09. theano',\n",
    "           '10. something interesting',\n",
    "           '11. useful tools'\n",
    "          ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "遍历文件夹得到所有的文件名："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "file_names = []\n",
    "\n",
    "for folder in folders:\n",
    "    files = sorted(os.listdir(folder))\n",
    "    file_names += [os.path.join(folder, file_name) for file_name in files if file_name.endswith('.ipynb')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def convert_to_files(names, to_format):\n",
    "    target_dir = os.path.join(\"static files\", to_format)\n",
    "    for folder in folders:\n",
    "        if not os.path.exists(os.path.join(target_dir, folder)):\n",
    "            os.makedirs(os.path.join(target_dir, folder))\n",
    "    converter = {\n",
    "        \"html\": nbconvert.export_html,\n",
    "        \"python\": nbconvert.export_python\n",
    "        }\n",
    "    \n",
    "    for file_name in names:\n",
    "        p = converter[to_format](file_name)\n",
    "        with open(os.path.join(target_dir, file_name[:-6] + p[1][\"output_extension\"]), 'w') as f:\n",
    "            f.write(p[0].encode(\"utf-8\"))\n",
    "        print file_name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转化 HTML 文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01. python tools/01.01 python overview.ipynb\n",
      "01. python tools/01.02 ipython interpreter.ipynb\n",
      "01. python tools/01.03 ipython notebook.ipynb\n",
      "01. python tools/01.04 use anaconda.ipynb\n",
      "02. python essentials/02.01 a tour of python.ipynb\n",
      "02. python essentials/02.02 python data types.ipynb\n",
      "02. python essentials/02.03 numbers.ipynb\n",
      "02. python essentials/02.04 strings.ipynb\n",
      "02. python essentials/02.05 indexing and slicing.ipynb\n",
      "02. python essentials/02.06 lists.ipynb\n",
      "02. python essentials/02.07 mutable and immutable data types.ipynb\n",
      "02. python essentials/02.08 tuples.ipynb\n",
      "02. python essentials/02.09 speed comparison between list & tuple.ipynb\n",
      "02. python essentials/02.10 dictionaries.ipynb\n",
      "02. python essentials/02.11 sets.ipynb\n",
      "02. python essentials/02.12 frozen sets.ipynb\n",
      "02. python essentials/02.13 how python assignment works.ipynb\n",
      "02. python essentials/02.14 if statement.ipynb\n",
      "02. python essentials/02.15 loops.ipynb\n",
      "02. python essentials/02.16 list comprehension.ipynb\n",
      "02. python essentials/02.17 functions.ipynb\n",
      "02. python essentials/02.18 modules and packages.ipynb\n",
      "02. python essentials/02.19 exceptions.ipynb\n",
      "02. python essentials/02.20 warnings.ipynb\n",
      "02. python essentials/02.21 file IO.ipynb\n",
      "03. numpy/03.01 numpy overview.ipynb\n",
      "03. numpy/03.02 matplotlib basics.ipynb\n",
      "03. numpy/03.03 numpy arrays.ipynb\n",
      "03. numpy/03.04 array types.ipynb\n",
      "03. numpy/03.05 array calculation method.ipynb\n",
      "03. numpy/03.06 sorting numpy arrays.ipynb\n",
      "03. numpy/03.07 array shapes.ipynb\n",
      "03. numpy/03.08 diagonals.ipynb\n",
      "03. numpy/03.09 data to & from string.ipynb\n",
      "03. numpy/03.10 array attribute & method overview .ipynb\n",
      "03. numpy/03.11 array creation functions.ipynb\n",
      "03. numpy/03.12 matrix object.ipynb\n",
      "03. numpy/03.13 general functions.ipynb\n",
      "03. numpy/03.14 vectorizing functions.ipynb\n",
      "03. numpy/03.15 binary operators.ipynb\n",
      "03. numpy/03.16 universal functions.ipynb\n",
      "03. numpy/03.17 choose.ipynb\n",
      "03. numpy/03.18 array broadcasting.ipynb\n",
      "03. numpy/03.19 reading and writing arrays.ipynb\n",
      "03. numpy/03.20 structured arrays.ipynb\n",
      "03. numpy/03.21 record arrays.ipynb\n",
      "03. numpy/03.22 memory maps.ipynb\n",
      "03. numpy/03.23 from matlab to numpy.ipynb\n",
      "04. scipy/04.01 scienticfic python overview.ipynb\n",
      "04. scipy/04.02 interpolation with scipy.ipynb\n",
      "04. scipy/04.03 statistics with scipy.ipynb\n",
      "04. scipy/04.04 curve fitting.ipynb\n",
      "04. scipy/04.05 minimization in python.ipynb\n",
      "04. scipy/04.06 integration in python.ipynb\n",
      "04. scipy/04.07 ODEs.ipynb\n",
      "04. scipy/04.08 sparse matrix.ipynb\n",
      "05. advanced python/05.01 overview of the sys module.ipynb\n",
      "05. advanced python/05.02 interacting with the OS - os.ipynb\n",
      "05. advanced python/05.03 comma separated values.ipynb\n",
      "05. advanced python/05.04 regular expression.ipynb\n",
      "05. advanced python/05.05 datetime.ipynb\n",
      "05. advanced python/05.06 sql databases.ipynb\n",
      "05. advanced python/05.07 object-relational mappers.ipynb\n",
      "05. advanced python/05.08 functions.ipynb\n",
      "05. advanced python/05.09 iterators.ipynb\n",
      "05. advanced python/05.10 generators.ipynb\n",
      "05. advanced python/05.11 context managers and the with statement.ipynb\n",
      "05. advanced python/05.12 decorators.ipynb\n",
      "05. advanced python/05.13 decorator usage.ipynb\n",
      "05. advanced python/05.14 the operator functools itertools toolz fn funcy module.ipynb\n",
      "05. advanced python/05.15 scope.ipynb\n",
      "05. advanced python/05.16 dynamic code execution.ipynb\n",
      "06. matplotlib/06.01 pyplot tutorial.ipynb\n",
      "06. matplotlib/06.02 customizing plots with style sheets.ipynb\n",
      "06. matplotlib/06.03  working with text - basic.ipynb\n",
      "06. matplotlib/06.04 working with text - math expression.ipynb\n",
      "06. matplotlib/06.05 image tutorial.ipynb\n",
      "06. matplotlib/06.06 annotating axes.ipynb\n",
      "06. matplotlib/06.07 legend.ipynb\n",
      "06. matplotlib/06.08 figures, subplots, axes and ticks.ipynb\n",
      "06. matplotlib/06.09 do not trust the defaults.ipynb\n",
      "06. matplotlib/06.10 different plots.ipynb\n",
      "07. interfacing with other languages/07.01 introduction.ipynb\n",
      "07. interfacing with other languages/07.02 python extension modules.ipynb\n",
      "07. interfacing with other languages/07.03 cython part 1.ipynb\n",
      "07. interfacing with other languages/07.04 cython part 2.ipynb\n",
      "07. interfacing with other languages/07.05 cython part 3.ipynb\n",
      "07. interfacing with other languages/07.06 cython part 4.ipynb\n",
      "07. interfacing with other languages/07.07 profiling with annotations.ipynb\n",
      "07. interfacing with other languages/07.08 ctypes.ipynb\n",
      "08. object-oriented programming/08.01 oop introduction.ipynb\n",
      "08. object-oriented programming/08.02 using oop model a forest fire.ipynb\n",
      "08. object-oriented programming/08.03 what is a object.ipynb\n",
      "08. object-oriented programming/08.04 writing classes.ipynb\n",
      "08. object-oriented programming/08.05 special method.ipynb\n",
      "08. object-oriented programming/08.06 properties.ipynb\n",
      "08. object-oriented programming/08.07 forest fire simulation.ipynb\n",
      "08. object-oriented programming/08.08 inheritance.ipynb\n",
      "08. object-oriented programming/08.09 super.ipynb\n",
      "08. object-oriented programming/08.10 refactoring the forest fire simutation.ipynb\n",
      "08. object-oriented programming/08.11 interfaces.ipynb\n",
      "08. object-oriented programming/08.12 public private special in python.ipynb\n",
      "08. object-oriented programming/08.13 multiple inheritance.ipynb\n",
      "09. theano/09.01 introduction and installation.ipynb\n",
      "09. theano/09.02 theano basics.ipynb\n",
      "09. theano/09.03 gpu on windows.ipynb\n",
      "09. theano/09.04 graph structures.ipynb\n",
      "09. theano/09.05 configuration settings and compiling modes.ipynb\n",
      "09. theano/09.06 conditions in theano.ipynb\n",
      "09. theano/09.07 loop with scan.ipynb\n",
      "09. theano/09.08 linear regression.ipynb\n",
      "09. theano/09.09 logistic regression .ipynb\n",
      "09. theano/09.10 softmax on mnist.ipynb\n",
      "09. theano/09.11 net on mnist.ipynb\n",
      "10. something interesting/10.01 maps using basemap.ipynb\n",
      "10. something interesting/10.02 maps using cartopy.ipynb\n",
      "11. useful tools/11.01 pprint.ipynb\n",
      "11. useful tools/11.02 pickle and cPickle.ipynb\n",
      "11. useful tools/11.03 json.ipynb\n"
     ]
    }
   ],
   "source": [
    "convert_to_files(file_names, \"html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "产生新目录："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with open('index.md') as f:\n",
    "    text = f.read()\n",
    "    with open(os.path.join(\"static files\", \"html\", \"README.md\"), \"w\") as g:\n",
    "        g.write(text.replace(\".ipynb\", \".html\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
